import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.beans.factory.support.SimpleBeanDefinitionRegistry;
import org.springframework.context.annotation.AnnotatedBeanDefinitionReader;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.example.test11.User;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.StandardEnvironment;

import java.util.Arrays;
import java.util.Properties;


/**
 * @Author: luyg
 * @Date: 2024/07/04/0:30
 * @Description:
 */
public class test {

	Logger logger= LoggerFactory.getLogger(test.class);

	/**
	 * 通过注解注册bean
	 */
	@Test
	public void test1(){
		BeanDefinitionRegistry beanDefinitionRegistry = new SimpleBeanDefinitionRegistry();
		AnnotatedBeanDefinitionReader annotatedBeanDefinitionReader = new AnnotatedBeanDefinitionReader(beanDefinitionRegistry);
		annotatedBeanDefinitionReader.register(User.class);
		logger.info(Arrays.toString(beanDefinitionRegistry.getBeanDefinitionNames()));
	}


	/**\
	 * 扫描包路径获取注册bean
	 */
	@Test
	public void test2(){
		BeanDefinitionRegistry beanDefinitionRegistry = new SimpleBeanDefinitionRegistry();
		ClassPathBeanDefinitionScanner annotatedBeanDefinitionReader = new ClassPathBeanDefinitionScanner(beanDefinitionRegistry);
		annotatedBeanDefinitionReader.scan("org.example");
		logger.info(Arrays.toString(beanDefinitionRegistry.getBeanDefinitionNames()));
	}

	@Test
	public void test3(){
		GenericBeanDefinition genericBeanDefinition = new GenericBeanDefinition();
		genericBeanDefinition.setBeanClassName("org.example.test11.User");
		MutablePropertyValues propertyValues = new MutablePropertyValues();
		propertyValues.addPropertyValue("id",1);
		propertyValues.addPropertyValue("name","gangan");
		genericBeanDefinition.setPropertyValues(propertyValues);
		logger.info(genericBeanDefinition.getBeanClassName());
	}

	@Test
	public void env(){
		//定义标准环境
		StandardEnvironment standardEnvironment = new StandardEnvironment();
		MutablePropertySources propertySources = standardEnvironment.getPropertySources();
		Properties properties = new Properties();
		properties.setProperty("age","121");
		PropertySource propertySource = new PropertiesPropertySource("gangan",properties);
		propertySources.addLast(propertySource);
		System.out.println(standardEnvironment.getProperty("age"));
	}



}
